<?php

namespace apps\category\models;

use Yii;
use \yii\db\ActiveRecord;
use \yii\db\Expression;

/**
 * This is the model class for table "category_category".
 *
 * @property integer $id
 * @property integer $type_id
 * @property string $code
 * @property string $name
 * @property integer $parent_id
 * @property integer $status
 * @property integer $position
 * @property string $create_time
 * @property string $update_time
 * @property string $image_url 
 * @property string $description 
 */
class Category extends ActiveRecord {

    const STATUS_VALID = 1;
    const STATUS_INVALID = 0;
    const STATUS_REMOVED = -1;

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'category_category';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['type_id', 'code', 'name'], 'required'],
            [['parent_id', 'status', 'position', 'type_id'], 'integer'],
            [['create_time', 'update_time'], 'safe'],
            [['code', 'name'], 'string', 'max' => 20],
            [['image_url'], 'file', 'types' => ['jpg', 'jpeg', 'gif', 'png']],
            [['description'], 'string', 'max' => 200]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'type_id' => '类别',
            'code' => '代码',
            'name' => '名称',
            'parent_id' => '上级分类',
            'status' => '状态',
            'position' => '顺序',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
            'image_url' => '图标',
            'description' => '描述',
        ];
    }

    /**
     * 所有状态
     * @return []
     */
    public static function allStatus() {
        return [
            self::STATUS_VALID => '有效',
            self::STATUS_INVALID => '无效',
            self::STATUS_REMOVED => '删除',
        ];
    }

    /**
     * 状态名称
     * @param string $index
     * @return string
     */
    public static function statusName($index) {
        $all = self::allStatus();
        return $all[$index];
    }

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'timestamp' => [
                'class' => 'yii\behaviors\TimestampBehavior',
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],
                ],
                'value' => new Expression('NOW()'),
            ],
        ];
    }

}
